<?php
	include_once 'isLogin.php';

	$table_name = "companies_accounting";
	$key_index = array('title','content','upid','state');
	
	$colors = array(
				'state'=>array('0'=>'#ff0000','1'=>'#16d316'),
			);
	$names = array(
				'state'=>array('0'=>'禁用','1'=>'可用'),
			);
                    
	//查询
	if($_GET['submit']=='搜索'){
		
		$name= $_GET['name'];
		$date = $_GET['date'];
	//判断搜索输入是否为空
		if($_GET[name]!='' && $_GET[name]!=null){
			$select_sql=" c.title like '%".$name."%'";
		}
                
               if($_GET['date']!='' && $_GET['date']!=null){
                   if($select_sql != ''&& $select_sql !=  null)
                   {
                       $select_sql .=" and a.date like '%".$date."%'";
                   }
		   else
                   {
                         $select_sql .=" a.date like '%".$date."%'";
		  }
               }
		
	   }

	//添加
	if($_GET['action']=='add'){
		
		//插入数据
		if(is_array($_POST['values'])){
			foreach($_POST['values'] as $k=>$v){
				$arr[$key_index[$k]] = $v;
			}
		}
		$commonc->insert($table_name,$arr);
		$id = $commonc->insert_id();
		
		//排序
		$arr = array('sort'=>$id);
		$commonc->update($table_name, $arr, "id='".$id."'");
		
		//查询记录
		$data = $commonc->getInfo($table_name, $id);
		
		if($id!=''){
			$return='<tr line_id="'.$id.'">
			<td> <input type="checkbox" name="checkbox" value="'.$id.'" /></td>
			<td value_id="'.$id.'">'.$data['title'].'</td>
			<td value_id="'.$id.'">'.$data['content'].'</td>
			<td>&nbsp;</td>
			<td class="xiugai">
				<a href="javascript:;" onclick="sort($(this),\''.$id.'\',\'up\',\''.$_basename.'?action=sort\')"><img src="images/sort-up.png"/></a>
				<a href="javascript:;" onclick="sort($(this),\''.$id.'\',\'down\','.$_basename.'?action=sort\')"><img src="images/sort-down.png"/></a>
			</td>
			<td class="xiugai"><a href="javascript:;" style="color: '.$colors['state'][$data['state']].'" onclick="state(\''.$id.'\',\'state\',\''.$_basename.'\',$(this))">'.$names['state'][$data['state']].'</a></td>
			<td class="xiugai">
			<a href="javascript:;" onclick="edit(\''.$id.'\',$(this),\''.$_basename.'?action=edit\')">修改</a>';
			if($data['upid']==false) $return.='<a href="'.$_basename.'?id='.$id.'">子类</a>';
			$return.='</td></tr>';
			$arr=array('error'=>'0','content'=>$return);
		}else{
			$arr=array('error'=>'1','content'=>'添加失败');
		}
		echo json_encode($arr);
		exit();
	}
	
	//删除
	if($_GET['action']=='del'){
		$delFile = array(
				array('key'=>'img','dir'=>'../upload/test/large/'),
				array('key'=>'img','dir'=>'../upload/test/middle/'),
				array('key'=>'img','dir'=>'../upload/test/small/'),
				array('key'=>'file','dir'=>'../upload/test/file/'),
		);
		echo $commonc->del($table_name,$_POST['id'],$delFile);
		exit();
	}
	
	//修改
	if($_GET['action']=='edit'){
		$arr = array($key_index[$_POST['index']]=>$_POST['value']);
		echo $commonc->update($table_name,$arr,"id='".$_POST['value_id']."'");
		exit();
	}
	
	//修改状态
	if($_GET['action']=='state'){
		echo $commonc->changeState($table_name,$_POST['id'],$_POST['key'],$colors[$_POST['key']],$names[$_POST['key']]);
		exit();
	}
	
	//排序
	if($_GET['action']=='sort'){
		if($_POST['type']=='down') $type = "prev";
		if($_POST['type']=='up') $type = "next";
		$sort = $commonc->sortChange($table_name, $_POST['id'], $type);
		echo $sort['id'];
		exit();
	}

	//得到所有内容
      
        $where = " where a.state = 2 ".$select_sql;
	$sql = $table_name." a left join company_order o on DATE_FORMAT(a.date,'%Y-%m')= DATE_FORMAT(o.order_date,'%Y-%m') and DATE_FORMAT(o.order_date,'%Y-%m')<> DATE_FORMAT(o.trade_success_date,'%Y-%m') and  a.company_id = o.company_id  left join companies c on a.company_id =c.id left join companies_detail cd on c.id = cd.company_id and cd.title_id = 5";
        $sql.= $where;
        $sql.= " group by account_date,company_id order by closing_date desc ";
        $key = "DATE_FORMAT(a.date,'%Y-%m') as account_date, DATE_FORMAT(o.order_date,'%Y-%m') as order_date ,a.company_id,a.sum_money,sum(o.pay_money)  as remain_money,a.this_profit, a.last_remainprofit,a.id,DATE_FORMAT(a.closing_date,'%Y-%m-%d') as closing_date,c.title,cd.content as phone";
        $row = $commonc->getList($sql,$sql, $key);
              
	
	
	//var_dump($row);
	//var_dump($row['result'][0]['id']);
	$smarty->assign("row",$row['result']);
	$smarty->assign('page',$row['page']);
        $smarty->assign('search_date',$date);
        $smarty->assign('search_name',$name);
	$smarty->display($_filename.".html");
	
        